Imports Janus.Windows.GridEX
Imports System.IO

Public Class frmORDER
#Region "Variables"
    Dim imgListFood As New ImageList()
    Dim imgListMenu As New ImageList()

    Dim dtMenuDetail As DataTable
    Dim dtBillDetail As DataTable = Nothing
    Dim dtBillDetailIndex As Integer = 0

    Dim SubTotal As Double = 0
    Dim DiscountAmt As Double = 0
    Dim TaxAmt As Double = 0
    Dim SVCAmt As Double = 0
    Dim Total As Double = 0
    Dim TipAmt As Double = 0

    Dim EmployeeCode As String = ""
    Dim BillNotes As String = ""
    Public BillDeposit As Double = 0

    Public CustomerCode As String = ""
    Public BillCode As Int64 = 0
    Public TBL_CODE As String = ""
    Public TBL_NAME As String = ""
    Public BillSeq As String = ""
    Public PRO_CODE As String = ""
    Public PRO_CODE_LINE As String = ""

#End Region
#Region "Control Event"
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If GridOrder.RowCount > 0 And BillCode = 0 Then
            BillCode = CreateOrder()
        End If
        If BillCode > 0 Then
            Dim frm As New frmPRINT
            frm.BillCode = BillCode
            frm.ShowDialog()
        End If
        txtBarcode.Focus()
    End Sub

    Private Sub btnAdmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdmin.Click
        Dim frm As New frmMANAGEMENT
        frm.ShowDialog()
        Select Case frm.Action
            Case 1 'TO-GO Select
                If frm.BILLCODE > 0 Then
                    Me.LoadBillDetail(frm.BILLCODE)
                End If
            Case 2 'SEARCH ITEM
                If frm.ITEMCODE <> "" Then
                    Dim dt As DataTable
                    Dim arName As New ArrayList
                    arName.Add("@Condition")
                    arName.Add("@Parameter")
                    Dim arVal As New ArrayList
                    arVal.Add(1)
                    arVal.Add(frm.ITEMCODE)
                    dt = BLL.ObjectGetBy("ITEM_SEARCH", arName, arVal)

                    If dt.Rows.Count = 1 Then
                        AddItem(dt.Rows(0).Item("ITEM_TYPE"), dt.Rows(0).Item("ITEM_CODE").ToString, _
                            dt.Rows(0).Item("ITEM_NAME").ToString, dt.Rows(0).Item("PRICE"), _
                            dt.Rows(0).Item("VAT_RATE"), dt.Rows(0).Item("ATTRIBUTE").ToString, dt.Rows(0).Item("ITEM_INV"), dt.Rows(0).Item("ITEM_STOCK"), 0)
                    End If
                End If
        End Select
        txtBarcode.Focus()
    End Sub

    Private Sub btnSettle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettle.Click
        If GridOrder.RowCount = 0 Then Return

        If BillCode <= 0 Then
            BillCode = CreateOrder()
            If BillCode < 0 Then
                MessageBox.Show("FAILED!")
                Return
            End If
        Else
            UpdateOrder()
        End If
        If BillCode > 0 Then
            Dim frm As New frmSETTLE
            frm.BillCode = BillCode
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                If PublicVariable.ApplicationType = 1 Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    NewOrder()
                End If
            End If
        Else
            MessageBox.Show("SAVE FAILED")
        End If
        txtBarcode.Focus()
    End Sub

    Private Sub btnQty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQty.Click
        If GridOrder.RowCount = 0 Then
            Return
        End If
        Dim frm As New frmNUMBER
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If GridOrder.CurrentRow Is Nothing Then
                GridOrder.Row = GridOrder.RowCount - 1
            End If
            Dim NewQty As Double = frm.txtNumber.Text
            Dim PRICE As Double = GridOrder.GetValue("PRICE")
            Dim TOTAL_ORG As Double = GridOrder.GetValue("TOTAL_ORG")
            Dim DSC_PER As Double = GridOrder.GetValue("DSC_PER")
            Dim DSC_AMT As Double = GridOrder.GetValue("DSC_AMT")
            Dim VAT_PER As Double = GridOrder.GetValue("VAT_PER")
            dtBillDetail.Rows(GridOrder.Row).Item("QUANTITY") = NewQty
            dtBillDetail.Rows(GridOrder.Row).Item("TOTAL_ORG") = NewQty * PRICE
            dtBillDetail.Rows(GridOrder.Row).Item("DSC_AMT") = TOTAL_ORG * DSC_PER / 100
            dtBillDetail.Rows(GridOrder.Row).Item("VAT_AMT") = (TOTAL_ORG - DSC_AMT) * VAT_PER / 100

            Dim arName As New ArrayList
            Dim arVal As New ArrayList
            arName.Clear()
            arVal.Clear()
            arName.Add("@CURR_CODE")
            arName.Add("@DATE")
            arName.Add("@LOCAL_CURR")
            arVal.Add(PublicVariable.LocalCurrency)
            arVal.Add(Now.Date)
            arVal.Add(PublicVariable.ForeignCurrency)
            Dim ExchangeRate As Double
            ExchangeRate = BLL.Object_Update_Result("EXCHANGERATE_GET", arName, arVal)
            dtBillDetail.Rows(GridOrder.Row).Item("FC_AMOUNT") = TOTAL_ORG * ExchangeRate
            UpdateTotal()
        End If
        txtBarcode.Focus()
    End Sub

    Private Sub btnCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCustomer.Click
        Dim frm As New frmSEARCH_CUSTOMER
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtCustomer.Text = frm.GridCustomer.GetValue("CUS_NAME").ToString
            Me.CustomerCode = frm.GridCustomer.GetValue("CUS_CODE").ToString
        End If
        txtBarcode.Focus()
        GC.Collect()
    End Sub

    Private Sub btnDiscout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiscount.Click
        If GridOrder.RowCount = 0 Then Return
        Dim frm As New frmDISCOUNT
        frm.LINE_TOTAL = GridOrder.GetValue("TOTAL_ORG")
        frm.BILL_TOTAL = SubTotal
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If frm.ckLine_Percent.Checked Or frm.ckLine_Amount.Checked Then
                GridOrder.SetValue("DSC_PER", frm.DiscountPercent)
                GridOrder.SetValue("DSC_AMT", GridOrder.GetValue("TOTAL_ORG") * frm.DiscountPercent / 100)
                GridOrder.SetValue("VAT_AMT", (GridOrder.GetValue("TOTAL_ORG") - GridOrder.GetValue("DSC_AMT")) * GridOrder.GetValue("VAT_PER") / 100)
            Else
                For i As Integer = 0 To GridOrder.RowCount - 1
                    GridOrder.Row = i
                    GridOrder.SetValue("DSC_PER", frm.DiscountPercent)
                    GridOrder.SetValue("DSC_AMT", GridOrder.GetValue("TOTAL_ORG") * frm.DiscountPercent / 100)
                    GridOrder.SetValue("VAT_AMT", (GridOrder.GetValue("TOTAL_ORG") - GridOrder.GetValue("DSC_AMT")) * GridOrder.GetValue("VAT_PER") / 100)
                Next
            End If
            GridOrder.UpdateData()
            UpdateTotal()
        End If
        txtBarcode.Focus()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        If PublicVariable.ApplicationType = 2 Then
            PublicVariable.frmLogin.Show()
        End If
    End Sub

    Private Sub btnTip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTip.Click
        If GridOrder.RowCount = 0 Then Return
        Dim frm As New frmNUMBER
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            TipAmt = frm.txtNumber.Text
        End If

        txtBarcode.Focus()
    End Sub

    Private Sub btnKitchenMemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKitchenMemo.Click
        If (Me.GridOrder.SelectedItems.Count = 0) Then Return
        Dim frm As New frmKITCHEN_MEMO
        Dim mm As String = GridOrder.SelectedItems.Item(0).GetRow.Cells("MEMO_DETAIL").Text
        If mm <> "" Then
            frm.txtMemo.Text = mm
        End If
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            GridOrder.SetValue("MEMO_DETAIL", frm.txtMemo.Text)
            dtBillDetail.Rows(GridOrder.Row).Item("MEMO_DETAIL") = frm.txtMemo.Text
        End If
        'txtBarcode.Focus()
    End Sub

    Private Sub btnEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployee.Click
        Dim frm As New frmEMPLOYEE

        If PublicVariable.BillEmployee = "1" Then
            'BY BILL
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                EmployeeCode = frm.GridEmployee.GetValue("EMP_CODE").ToString
            Else
                EmployeeCode = ""
            End If
        Else
            'BY LINE
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                dtBillDetail.Rows(GridOrder.Row).Item("EMP_CODE") = frm.GridEmployee.GetValue("EMP_CODE").ToString

            Else
                dtBillDetail.Rows(GridOrder.Row).Item("EMP_CODE") = ""

            End If
            UpdateTotal()
        End If

        txtBarcode.Focus()
    End Sub

    Private Sub frmORDER_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            GC.Collect()
            Me.Dispose()
        Catch ex As Exception

        End Try
    End Sub


    Private Sub frmORDER_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'TODO: This line of code loads data into the 'TOTALPOSDataSet.BILL_DETAIL' table. You can move, or remove it, as needed.

        Dim r As Rectangle
        r = Me.GridMenu.GetCardBounds(GridMenu.Row)
        r.Height = 300

        If PublicVariable.ApplicationType = 2 Then
            Me.lblTable.Visible = False
            Me.txtTBL_NAME.Visible = False
            Me.lblGuest.Visible = False
            Me.txtGuest.Visible = False
        End If
        Me.GridOrder.RootTable.Columns("QUANTITY").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("TOTAL_ORG").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("PRICE").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("TOTAL_ORG").TotalFormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("FC_AMOUNT").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("LINE_TOTAL").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("VAT_AMT").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("DSC_PER").FormatString = PublicVariable.NumberFormatString
        Me.GridOrder.RootTable.Columns("DSC_AMT").FormatString = PublicVariable.NumberFormatString
        PublicFunction.NewFormLoad(Me)
        PublicVariable.frmSecondScrn.pnBill.Closed = False
        PublicVariable.frmSecondScrn.txtMessage.Text = "THANK YOU FOR YOUR SHOPPING!"
        GridMenu.DataSource = oOrder.LoadAllMenu.DefaultView
        dtMenuDetail = oOrder.LoadAllMenuDetail
        GridMenu_Click(sender, e)
        txtSalesDate.Text = PublicVariable.SystemDate.ToString(PublicVariable.DateTimeFormat)
        If BillCode = 0 Then
            txtBILL_NUM.Text = oOrder.BILL_GETNUMBER()
        End If
        If File.Exists(PublicVariable.LogoFile) Then
            PbLogo.BackgroundImage = New Bitmap(PublicVariable.LogoFile)
        End If
        LoadBillDetail(BillCode)
        txtBarcode.Focus()
    End Sub

    Private Sub GridMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridMenu.Click
        Dim dv As New DataView(dtMenuDetail)
        dv.RowFilter = "MNU_Code='" + GridMenu.GetValue("MNU_Code").ToString + "'"
        GridMenuDetail.DataSource = dv
        txtBarcode.Focus()
    End Sub

    Private Sub GridMenuDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ItemCode As String = IIf(GridMenuDetail.GetValue("ITEM_CODE") Is DBNull.Value, "", GridMenuDetail.GetValue("ITEM_CODE"))
        If ItemCode <> "" Then
            Dim ItemType As Integer = GridMenuDetail.GetValue("ITEM_TYPE")
            Dim ItemName As String = GridMenuDetail.GetValue("ITEM_NAME").ToString
            Dim Price As Double = GridMenuDetail.GetValue("PRICE")
            Dim VAT_Rate As Double = GridMenuDetail.GetValue("VAT_PER")
            Dim ATTRIBUTE As String = GridMenuDetail.GetValue("ATTRIBUTE").ToString
            Dim ITEM_INV As Boolean = GridMenuDetail.GetValue("ITEM_INV")
            Dim ITEM_STOCK As Integer = GridMenuDetail.GetValue("ITEM_STOCK")
            GridOrder.MoveLast()
            Dim LastBD_SEQ As Integer = IIf(GridOrder.GetValue("BD_SEQ") Is DBNull.Value, 0, GridOrder.GetValue("BD_SEQ"))
            AddItem(ItemType, ItemCode, ItemName, Price, VAT_Rate, ATTRIBUTE, ITEM_INV, ITEM_STOCK, LastBD_SEQ)
            txtBarcode.Focus()
        End If

    End Sub
    Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If GridOrder.RowCount > 0 Then
            If GridOrder.CurrentRow Is Nothing Then
                GridOrder.Row = 0
            End If
            Dim BD_CODE As Integer = GridOrder.GetValue("BD_Code")
            If BD_CODE <> 0 Then
                If SystemFunction.ShowYesNoForm("REMOVE SAVED ITEM?", "") = Windows.Forms.DialogResult.Yes Then
                    oPrint.PRINT_KITCHEN_ADD(BillCode, GridOrder.GetValue("ITEM_CODE").ToString, _
                        GridOrder.GetValue("ITEM_NAME").ToString, (-1) * GridOrder.GetValue("QUANTITY"), _
                        GridOrder.GetValue("KEEP_PACK").ToString, GridOrder.GetValue("MEMO_DETAIL").ToString)

                    For Each dr As GridEXRow In GridOrder.CurrentRow.GetChildRows
                        oOrder.BILLDETAIL_CANCEL(dr.Cells("BD_Code").Value)
                        dtBillDetail.Rows(dr.AbsolutePosition).Delete()
                        'AbsolutePosition: posion in grid, not in dr
                    Next
                    oOrder.BILLDETAIL_CANCEL(BD_CODE) 'Xoa trong database
                    dtBillDetail.Rows(GridOrder.Row).Delete() 'xoa tren datatable
                    'GridOrder.Row: row tren Dr, not tren Grid
                End If
            Else
                dtBillDetail.Rows(GridOrder.Row).Delete() 'xoa tren datatable
            End If
            UpdateTotal()
        End If

        txtBarcode.Focus()
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If BillCode > 0 Then
            'Update bill
            UpdateOrder()
        Else
            'Create bill
            BillCode = CreateOrder()
        End If
        If BillCode = -1 Then
            MessageBox.Show("ERROR!")
        End If
        Dim oPrint As New oPrint

        If PublicVariable.OrderPrintAuto = "1" Then
            oPrint.PrintSlip(BillCode, False, PublicVariable.OrderPrintFilePath, PublicVariable.OrderPrintName, PublicVariable.OrderPrintSlipCount)
        End If
        If PublicVariable.KitchenPrintAuto = "1" Then
            Dim dt As DataTable
            dt = oPrint.LOAD_PRINT_KITCHEN(BillCode)
            For i As Integer = 0 To dt.Rows.Count - 1
                oPrint.Print_KITCHEN(BillCode, False, PublicVariable.KitchenPrintFilePath, dt.Rows(i).Item("PRINTER").ToString)
            Next
        End If
        If PublicVariable.ApplicationType = 1 Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Else
            NewOrder()
            txtBarcode.Focus()
        End If

    End Sub
    Private Sub GridOrder_FormattingRow(ByVal sender As System.Object, ByVal e As Janus.Windows.GridEX.RowLoadEventArgs) Handles GridOrder.FormattingRow
        If e.Row.RowType = RowType.TotalRow Then
            e.Row.Cells("ITEM_NAME").Text = "SUB TOTAL"
        End If
    End Sub
    Private Sub txtCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustomer.Click
        If CustomerCode <> "" Then
            If SystemFunction.ShowYesNoForm("DO YOU WANT TO CLEAR CUSTOMER", "") = Windows.Forms.DialogResult.Yes Then
                txtCustomer.Text = ""
                Me.CustomerCode = ""
            End If
        End If
        txtBarcode.Focus()
    End Sub

    Private Sub txtCustomer_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetail.Click
        If CustomerCode = "" Then Return
        Dim frm As New frmCUSTOMER_DETAIL
        Dim arName As New ArrayList
        arName.Add("@Condition")
        arName.Add("@Parameter")
        Dim arVal As New ArrayList
        arVal.Add(1)
        arVal.Add(CustomerCode)
        Dim dt As DataTable = BLL.ObjectGetBy("CUSTOMER_SEARCH", arName, arVal)
        Dim dv As New DataView(dt)
        frm.dr_customer = dv(0)
        frm.CUS_CODE = CustomerCode
        frm.ShowDialog()
        txtBarcode.Focus()
    End Sub

    Private Sub btnKeep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeep.Click
        If GridOrder.RowCount = 0 Then
            Return
        End If
        If GridOrder.CurrentRow Is Nothing Then
            GridOrder.Row = GridOrder.RowCount - 1
        End If
        If GridOrder.CurrentRow.Cells("KEEP_PACK").Text = "K" Then
            GridOrder.SetValue("KEEP_PACK", "")
        Else
            GridOrder.SetValue("KEEP_PACK", "K")
            Dim frm As New frmKEEPING
            If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                GridOrder.SetValue("CUSTOMER_NAME", frm.txtCustomerName.Text)
                GridOrder.SetValue("PERCENT", frm.txtPercent.Text)
                GridOrder.SetValue("REMARK", frm.txtRemark.Text)
            End If
        End If
        GridOrder.UpdateData()
        txtBarcode.Focus()
    End Sub

    Private Sub btnPack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPack.Click
        If GridOrder.RowCount = 0 Then
            Return
        End If
        If GridOrder.CurrentRow Is Nothing Then
            GridOrder.Row = GridOrder.RowCount - 1
        End If
        If GridOrder.GetValue("KEEP_PACK").ToString = "P" Then
            GridOrder.SetValue("KEEP_PACK", "")
        Else
            GridOrder.SetValue("KEEP_PACK", "P")
        End If
        GridOrder.UpdateData()
        txtBarcode.Focus()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If BillCode > 0 Then
            If SystemFunction.ShowYesNoForm("ARE YOU SURE YOU WANT TO CANCEL THIS BILL?", "") = Windows.Forms.DialogResult.Yes Then
                oOrder.BILL_CANCEL(BillCode)
                If PublicVariable.ApplicationType = 1 Then
                    Me.Close()
                Else
                    NewOrder()
                End If
            End If

        End If
    End Sub
    Private Sub txtGuest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGuest.Click
        Dim frm As New frmNUMBER
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtGuest.Text = frm.txtNumber.Text
            If txtGuest.Text = "" Then
                txtGuest.Text = "1"
            End If
        End If
        txtBarcode.Focus()
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        PublicFunction.GRID_UP_DOWN(1, Me.GridOrder)
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        PublicFunction.GRID_UP_DOWN(0, Me.GridOrder)
    End Sub
    Private Sub btnChangePrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangePrice.Click
        If GridOrder.CurrentRow Is Nothing Then Return
        Dim frm As New frmOpenItem
        frm.txtItemName.Enabled = False
        frm.txtItemName.Text = GridOrder.GetValue("ITEM_NAME").ToString
        frm.txtPrice.Text = String.Format(PublicVariable.NumberFormatString, GridOrder.GetValue("PRICE"))
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            If IsNumeric(frm.txtPrice.Text) Then
                Dim PRICE As Double = frm.txtPrice.Text
                Dim QUANTITY As Double = GridOrder.GetValue("QUANTITY")
                Dim VAT_PER As Double = GridOrder.GetValue("VAT_PER")

                dtBillDetail.Rows(GridOrder.Row).Item("PRICE") = PRICE
                dtBillDetail.Rows(GridOrder.Row).Item("TOTAL_ORG") = PRICE * QUANTITY
                dtBillDetail.Rows(GridOrder.Row).Item("DSC_PER") = 0
                dtBillDetail.Rows(GridOrder.Row).Item("DSC_AMT") = 0
                dtBillDetail.Rows(GridOrder.Row).Item("VAT_AMT") = VAT_PER * PRICE * QUANTITY / 100

                'GridOrder.SetValue("PRICE", PRICE)
                'GridOrder.SetValue("TOTAL_ORG", PRICE * QUANTITY)
                'GridOrder.SetValue("DSC_PER", 0)
                'GridOrder.SetValue("DSC_AMT", 0)
                'GridOrder.SetValue("VAT_AMT", VAT_PER * PRICE * QUANTITY / 100)
                UpdateTotal()
            End If
        End If
    End Sub

    Private Sub btnDutyFree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDutyFree.Click
        If GridOrder.CurrentRow Is Nothing Then Return
        If GridOrder.GetValue("VAT_AMT") = 0 Then
            'ADD VAT
            GridOrder.SetValue("VAT_AMT", GridOrder.GetValue("TOTAL_ORG") * GridOrder.GetValue("VAT_PER") / 100)
        Else
            'REMOVE VAT
            GridOrder.SetValue("VAT_AMT", 0)
        End If

        UpdateTotal()
    End Sub

    Private Sub btnFree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFree.Click
        GridOrder.SetValue("DSC_PER", 100)
        GridOrder.SetValue("DSC_AMT", GridOrder.GetValue("TOTAL_ORG"))
        GridOrder.SetValue("VAT_AMT", 0)
        UpdateTotal()
    End Sub

    Private Sub btnVoidItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVoidItem.Click
        GridOrder.SetValue("PRICE", (-1) * GridOrder.GetValue("PRICE"))
        GridOrder.SetValue("TOTAL_ORG", (-1) * GridOrder.GetValue("TOTAL_ORG"))
        GridOrder.SetValue("VAT_AMT", (-1) * GridOrder.GetValue("VAT_AMT"))
        GridOrder.SetValue("DSC_AMT", (-1) * GridOrder.GetValue("DSC_AMT"))
        UpdateTotal()
    End Sub
    Private Sub btnSearchItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchItem.Click
        Dim frm As New frmSEARCH_ITEM
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            If frm.GridItem.CurrentRow Is Nothing Then Return
            Dim dr As DataRowView = CType(frm.GridItem.CurrentRow.DataRow, DataRowView)
            AddItem(dr.Item("ITEM_TYPE"), dr.Item("ITEM_CODE").ToString, _
                    dr.Item("ITEM_NAME").ToString, dr.Item("PRICE"), _
                    dr.Item("VAT_RATE"), dr.Item("ATTRIBUTE").ToString, dr.Item("ITEM_INV"), dr.Item("ITEM_STOCK"), 0)
        End If
    End Sub
    Private Sub btnSubItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubItem.Click
        Dim arName As New ArrayList
        arName.Add("@ITEM_CODE")
        Dim arVal As New ArrayList
        arVal.Add(dtBillDetail.Rows(GridOrder.Row).Item("ITEM_CODE").ToString)
        Dim dt1 As DataTable = BLL.ObjectGetBy("SUBITEM_LOADBYITEM", arName, arVal)
        If dt1.Rows.Count > 0 Then
            Dim frm2 As New frmSUBITEM
            frm2.GridSubItem.DataSource = dt1.DefaultView
        End If
        txtBarcode.Focus()
    End Sub

    Private Sub btnEndService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEndService.Click
        If GridOrder.RowCount = 0 Then Return
        dtBillDetail.Rows(GridOrder.Row).Item("TIME_OUT") = CStr(Now.Hour) + ":" + CStr(Now.Minute)
        UpdateTotal()
        txtBarcode.Focus()
    End Sub

    Private Sub btnAttribute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttribute.Click
        If GridOrder.RowCount = 0 Then Return
        Dim at As String = dtBillDetail.Rows(GridOrder.Row).Item("ATTRIBUTE").ToString
        Dim frm As New frmATTRIBUTE
        frm.Attribute = at
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            dtBillDetail.Rows(GridOrder.Row).Item("ATTRIBUTE") = frm.attribute
            UpdateTotal()
        End If
    End Sub

    Private Sub btnNotes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNotes.Click
        Dim frm As New frmFREETEXT
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            BillNotes = frm.txtNotes.Text
        End If
    End Sub
#End Region
#Region "Functions"


    Private Sub AddItem(ByVal ItemType As Integer, ByVal ItemCode As String, _
    ByVal ItemName As String, ByVal Price As Double, ByVal VAT_Rate As Double, _
    ByVal ATTRIBUTE As String, ByVal ITEM_INV As Boolean, ByVal ITEM_STOCK As Double, _
    ByVal LastBD_SEQ As Integer)
       
        If PublicVariable.NegativeInventory = "0" Then
            'Check Stock
            If ITEM_INV And ITEM_STOCK <= 0 Then
                SystemFunction.ShowMessageForm("NO STOCK!", "")
                Return
            End If
        End If
        Dim dt As DataTable
        Dim arName As New ArrayList
        arName.Add("@ITEM_CODE")
        arName.Add("@CUS_CODE")
        arName.Add("@Date")
        arName.Add("@Time")
        Dim arVal As New ArrayList
        arVal.Add(ItemCode)
        arVal.Add(CustomerCode)
        arVal.Add(PublicVariable.SystemDate)
        arVal.Add(Now.Hour & ":" & Now.Minute)
        dt = BLL.ObjectGetBy("PROMOTION_LOAD", arName, arVal)
        If dt.Rows.Count > 0 Then
            Dim frm As New frmPROMOTION
            frm.GridPromotion.DataSource = dt.DefaultView

            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'ADD PROMOTION ITEM
                If frm.GridPromotion.CurrentRow Is Nothing Then
                    Return
                End If
                If frm.GridPromotion.CurrentRow.RowType = RowType.GroupHeader Then
                    frm.GridPromotion.Row = frm.GridPromotion.Row + 1
                End If
                PRO_CODE_LINE = frm.GridPromotion.GetValue("PRO_CODE").ToString
                Dim dv As New DataView(dt)
                dv.RowFilter = "PRO_CODE='" + PRO_CODE_LINE + "' AND PRO_GROUP='" + frm.GridPromotion.GetValue("PRO_GROUP").ToString + "'"
                For Each dr As DataRowView In dv
                    Price = dr.Item("NEW_PRICE") / (1 + VAT_Rate / 100)
                    Dim dr1 As DataRow = dtBillDetail.NewRow
                    dr1("ITEM_CODE") = dr.Item("ITEM_CODE").ToString
                    dr1("ITEM_NAME") = dr.Item("ITEM_NAME").ToString
                    dr1("QUANTITY") = dr.Item("QUANTITY").ToString
                    dr1("TOTAL_ORG") = Price
                    dr1("VAT_PER") = VAT_Rate
                    dr1("VAT_AMT") = Price * VAT_Rate / 100
                    dr1("PRICE") = Price
                    dr1("BD_CODE") = 0
                    dr1("DSC_PER") = 0
                    dr1("DSC_AMT") = 0
                    dtBillDetail.Rows.Add(dr1)
                Next
                UpdateTotal()
                Exit Sub
            End If
        End If

        Dim Col0 As GridEXColumn = Me.GridOrder.RootTable.Columns("ITEM_CODE")
        If GridOrder.FindAll(Col0, ConditionOperator.Equal, ItemCode) > 0 _
                And ItemType = 0 Then
            'Khong tang so luong doi voi mon Open Item va Set Item
            Dim DSC_PER As Double = GridOrder.GetValue("DSC_PER")
            Dim TOTAL_ORG As Double
            Dim DSC_AMT As Double
            GridOrder.SetValue("QUANTITY", GridOrder.GetValue("QUANTITY") + 1)
            GridOrder.SetValue("TOTAL_ORG", GridOrder.GetValue("QUANTITY") * Price)
            TOTAL_ORG = GridOrder.GetValue("QUANTITY") * Price
            DSC_AMT = TOTAL_ORG * DSC_PER / 100
            GridOrder.SetValue("DSC_AMT", DSC_AMT)
            GridOrder.SetValue("VAT_AMT", (TOTAL_ORG - DSC_AMT) * GridOrder.GetValue("VAT_PER") / 100)
            GridOrder.UpdateData()
        Else
            If ItemType = 1 Then
                Dim frm1 As New frmOpenItem
                frm1.txtItemName.Text = ItemName
                If frm1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    ItemName = frm1.txtItemName.Text
                    Price = frm1.txtPrice.Value
                    Dim dr As DataRow = dtBillDetail.NewRow
                    dr("ITEM_CODE") = ItemCode
                    dr("ITEM_NAME") = ItemName
                    dr("QUANTITY") = 1
                    dr("TOTAL_ORG") = Price
                    dr("VAT_PER") = VAT_Rate
                    dr("VAT_AMT") = Price * VAT_Rate / 100
                    dr("PRICE") = Price
                    dr("DSC_PER") = 0
                    dr("DSC_AMT") = 0
                    dr("BD_CODE") = 0
                    dtBillDetail.Rows.Add(dr)
                End If
            ElseIf ItemType = 2 Then
                arName.Clear()
                arName.Add("@ITEM_CODE")
                arVal.Clear()
                arVal.Add(ItemCode)

                Dim dt1 As DataTable = BLL.ObjectGetBy("SUBITEM_LOADBYITEM", arName, arVal)
                If dt1.Rows.Count > 0 Then
                    Dim frm2 As New frmSUBITEM
                    frm2.GridSubItem.DataSource = dt1.DefaultView
                    If frm2.ShowDialog = Windows.Forms.DialogResult.OK Then
                        If GridOrder.RowCount > 0 Then
                            GridOrder.Row = GridOrder.RowCount - 1
                        End If
                        LastBD_SEQ = LastBD_SEQ + 1
                        Dim PARENT_CODE As Integer = LastBD_SEQ
                        Dim dr As DataRow = dtBillDetail.NewRow
                        dr("ITEM_CODE") = ItemCode
                        dr("ITEM_NAME") = ItemName
                        dr("QUANTITY") = 1
                        dr("TOTAL_ORG") = Price
                        dr("VAT_PER") = VAT_Rate
                        dr("VAT_AMT") = Price * VAT_Rate / 100
                        dr("PRICE") = Price
                        dr("BD_CODE_REF") = PARENT_CODE
                        'dr("BD_SEQ") = PARENT_CODE
                        dr("BD_CODE") = 0
                        dr("DSC_PER") = 0
                        dr("DSC_AMT") = 0
                        dtBillDetail.Rows.Add(dr)
                        For i As Integer = 0 To frm2.GridSelect.RowCount - 1
                            frm2.GridSelect.Row = i
                            Dim dr1 As DataRow = dtBillDetail.NewRow
                            dr1("SUBITEM_CODE") = frm2.GridSelect.GetValue("SUBITEM_CODE").ToString
                            dr1("ITEM_CODE") = ItemCode
                            dr1("ITEM_NAME") = frm2.GridSelect.GetValue("SUBITEM_NAME").ToString
                            dr1("QUANTITY") = 1
                            dr1("TOTAL_ORG") = 0
                            dr1("VAT_PER") = 0
                            dr1("VAT_AMT") = 0
                            dr1("PRICE") = 0
                            dr1("BD_CODE") = 0
                            dr1("PARENT_CODE") = PARENT_CODE
                            dr1("DSC_PER") = 0
                            dr1("DSC_AMT") = 0
                            dtBillDetail.Rows.Add(dr1)
                        Next
                    End If
                End If
            Else
                Dim dr As DataRow = dtBillDetail.NewRow
                dr("ITEM_CODE") = ItemCode
                dr("ITEM_NAME") = ItemName
                dr("QUANTITY") = 1
                dr("TOTAL_ORG") = Price
                dr("VAT_PER") = VAT_Rate
                dr("VAT_AMT") = Price * VAT_Rate / 100
                dr("PRICE") = Price
                dr("DSC_PER") = 0
                dr("DSC_AMT") = 0
                dr("BD_CODE") = 0
                dtBillDetail.Rows.Add(dr)
            End If
        End If
        UpdateTotal()
    End Sub
    Private Function CreateOrder() As Int64
        Dim dt As DataTable
        Dim PROMOTION_DISCOUNT As Double = 0

        Dim arName As New ArrayList
        arName.Add("@Amount")
        arName.Add("@CUS_CODE")
        arName.Add("@Date")
        Dim arVal As New ArrayList
        arVal.Add(Total)
        arVal.Add(CustomerCode)
        arVal.Add(PublicVariable.SystemDate)
        dt = BLL.ObjectGetBy("PROMOTION_LOAD_SETTLE", arName, arVal)

        If dt.Rows.Count > 0 Then
            PROMOTION_DISCOUNT = dt.Rows(0).Item("DISC_PER")
            PRO_CODE = dt.Rows(0).Item("PRO_CODE").ToString
        End If
        GridOrder.ExpandRecords()
        Dim PARENT_CODE As String = ""
        BillCode = oOrder.BillAdd(CustomerCode, PublicVariable.SystemDate, SubTotal, _
            TaxAmt, 0, 0, SVCAmt, Total, EmployeeCode, 1, "", PublicVariable.UserCode, _
            TBL_CODE, TipAmt, PRO_CODE, CInt(txtGuest.Text), BillNotes, BillDeposit)
        If BillCode > 0 Then
            For i As Integer = 0 To dtBillDetail.Rows.Count - 1 ' GridOrder.RowCount - 1
                'GridOrder.Row = i
                'Dim tempRow As GridEXRow = Me.GridOrder.GetRow(i)
                'If tempRow.RowType = RowType.Record Then
                Dim ItemCode As String = dtBillDetail.Rows(i).Item("ITEM_CODE").ToString
                Dim ItemName As String = dtBillDetail.Rows(i).Item("ITEM_NAME").ToString
                Dim Quantity As Integer = dtBillDetail.Rows(i).Item("QUANTITY")
                Dim DiscountPercent As Double = dtBillDetail.Rows(i).Item("DSC_PER")
                Dim DiscountAmount As Double = dtBillDetail.Rows(i).Item("DSC_AMT")
                Dim VAT_Rate As Double = dtBillDetail.Rows(i).Item("VAT_PER")
                Dim VAT_Amt As Double = dtBillDetail.Rows(i).Item("VAT_AMT")
                Dim MEMO_DETAIL As String = dtBillDetail.Rows(i).Item("MEMO_DETAIL").ToString
                Dim KEEP_PACK As String = dtBillDetail.Rows(i).Item("KEEP_PACK").ToString
                Dim Price As Double = dtBillDetail.Rows(i).Item("PRICE")
                Dim TOTAL_ORG As Double = dtBillDetail.Rows(i).Item("TOTAL_ORG")
                Dim EMP_CODE As String = dtBillDetail.Rows(i).Item("EMP_CODE").ToString
                Dim SUBITEM_CODE As String = dtBillDetail.Rows(i).Item("SUBITEM_CODE").ToString
                Dim ATTRIBUTE As String = dtBillDetail.Rows(i).Item("ATTRIBUTE").ToString
                If PROMOTION_DISCOUNT > 0 Then
                    DiscountPercent = PROMOTION_DISCOUNT
                End If
                Dim BD_Code As Integer = 0
                If SUBITEM_CODE = "" Then
                    BD_Code = oOrder.BillDetailAdd(BillCode, ItemCode, ItemName, Price, _
                    Quantity, DiscountPercent, _
                    VAT_Rate, KEEP_PACK, _
                    MEMO_DETAIL, ATTRIBUTE, PublicVariable.UserCode, SUBITEM_CODE, "", PRO_CODE_LINE, EMP_CODE)
                Else
                    BD_Code = oOrder.BillDetailAdd(BillCode, ItemCode, ItemName, Price, _
                                            Quantity, DiscountPercent, _
                                            VAT_Rate, KEEP_PACK, _
                                            MEMO_DETAIL, ATTRIBUTE, PublicVariable.UserCode, _
                                            SUBITEM_CODE, PARENT_CODE, PRO_CODE_LINE, EMP_CODE)
                End If

                If SUBITEM_CODE = "" Then 'Save BD_CODE for father item
                    PARENT_CODE = BD_Code
                End If

                oPrint.PRINT_KITCHEN_ADD(BillCode, ItemCode, ItemName, Quantity, KEEP_PACK, MEMO_DETAIL)

                If KEEP_PACK = "K" Then
                    Dim CustomerName As String = dtBillDetail.Rows(i).Item("CUSTOMER_NAME").ToString
                    Dim Remark As String = dtBillDetail.Rows(i).Item("REMARK").ToString
                    Dim Percent As String = dtBillDetail.Rows(i).Item("PERCENT").ToString

                    arName.Clear()
                    arName.Add("@CUSTOMER_NAME")
                    arName.Add("@REMARK")
                    arName.Add("@PERCENT")
                    arName.Add("@BILL_CODE")
                    arName.Add("@ITEM_CODE")
                    arName.Add("@ITEM_NAME")
                    arVal.Clear()
                    arVal.Add(CustomerName)
                    arVal.Add(Remark)
                    arVal.Add(Percent)
                    arVal.Add(BillCode)
                    arVal.Add(ItemCode)
                    arVal.Add(ItemName)
                    dt = BLL.ObjectGetBy("KEEPING_ADD", arName, arVal)
                    'BLL.ReturnXML("KEEPING", dt)

                End If
                If BD_Code <= 0 Then
                    MessageBox.Show("FAILED")
                    Return -1
                End If
                'End If
            Next
            Return BillCode
        Else
            Return -1
        End If
    End Function
    Private Sub UpdateOrder()
        GridOrder.ExpandRecords()
        Dim PARENT_CODE As String
        oOrder.BillUpdate(CustomerCode, PublicVariable.SystemDate, EmployeeCode, 1, "", PublicVariable.UserCode, _
                        BillCode, TipAmt, PRO_CODE, CInt(txtGuest.Text), BillNotes, BillDeposit)
        Dim PROMOTION_DISCOUNT As Double = 0
        Dim dt As DataTable
        Dim arName As New ArrayList
        arName.Add("@Amount")
        arName.Add("@CUS_CODE")
        arName.Add("@Date")
        Dim arVal As New ArrayList
        arVal.Add(Total)
        arVal.Add(CustomerCode)
        arVal.Add(PublicVariable.SystemDate)
        dt = BLL.ObjectGetBy("PROMOTION_LOAD_SETTLE", arName, arVal)

        If dt.Rows.Count > 0 Then
            PROMOTION_DISCOUNT = dt.Rows(0).Item("DISC_PER")
            PRO_CODE = dt.Rows(0).Item("PRO_CODE").ToString
        Else
            PROMOTION_DISCOUNT = 0
        End If
        For i As Integer = 0 To GridOrder.RowCount - 1
            GridOrder.Row = i
            If GridOrder.CurrentRow.RowType = RowType.Record Then
                Dim ItemCode As String = GridOrder.GetValue("ITEM_CODE").ToString
                Dim ItemName As String = GridOrder.GetValue("ITEM_NAME").ToString
                Dim Quantity As Integer = GridOrder.GetValue("QUANTITY")
                Dim DiscountPercent As Double = GridOrder.GetValue("DSC_PER")
                Dim DiscountAmount As Double = GridOrder.GetValue("DSC_AMT")
                Dim VAT_Rate As Double = GridOrder.GetValue("VAT_PER")
                Dim VAT_Amt As Double = GridOrder.GetValue("VAT_AMT")
                Dim MEMO_DETAIL As String = GridOrder.CurrentRow.Cells("MEMO_DETAIL").Text
                Dim KEEP_PACK As String = GridOrder.CurrentRow.Cells("KEEP_PACK").Text
                Dim Price As Double = GridOrder.GetValue("PRICE")
                Dim TOTAL_ORG As Double = GridOrder.GetValue("TOTAL_ORG")
                Dim BD_Code As Integer = GridOrder.GetValue("BD_CODE")
                Dim SUBITEM_CODE As String = GridOrder.CurrentRow.Cells("SUBITEM_CODE").Text
                Dim TIME_OUT As String = GridOrder.CurrentRow.Cells("TIME_OUT").Text
                Dim EMP_CODE As String = GridOrder.CurrentRow.Cells("EMP_CODE").Text
                Dim ATTRIBUTE As String = "" 'dtBillDetail.Rows(i).Item("ATTRIBUTE").ToString
                If PRO_CODE <> "" Then
                    DiscountPercent = PROMOTION_DISCOUNT
                End If

                If BD_Code > 0 Then
                    oOrder.BillDetailUpdate(BD_Code, ItemCode, ItemName, Price, _
                                                            Quantity, DiscountPercent, _
                                                            VAT_Rate, KEEP_PACK, _
                                                            MEMO_DETAIL, ATTRIBUTE, PublicVariable.UserCode, EMP_CODE, TIME_OUT)
                Else
                    If SUBITEM_CODE = "" Then
                        BD_Code = oOrder.BillDetailAdd(BillCode, ItemCode, ItemName, Price, _
                        Quantity, DiscountPercent, _
                        VAT_Rate, KEEP_PACK, _
                        MEMO_DETAIL, "", PublicVariable.UserCode, SUBITEM_CODE, "", PRO_CODE_LINE, EMP_CODE)
                    Else
                        BD_Code = oOrder.BillDetailAdd(BillCode, ItemCode, ItemName, Price, _
                                                Quantity, DiscountPercent, _
                                                VAT_Rate, KEEP_PACK, _
                                                MEMO_DETAIL, ATTRIBUTE, PublicVariable.UserCode, _
                                                SUBITEM_CODE, PARENT_CODE, PRO_CODE_LINE, EMP_CODE)
                    End If

                    If SUBITEM_CODE = "" Then 'Save BD_CODE for father item
                        PARENT_CODE = BD_Code
                    End If
                    If BD_Code > 0 Then
                        GridOrder.SetValue("BD_CODE", BD_Code)
                    Else
                        MessageBox.Show("FAILED")
                    End If
                End If
                oPrint.PRINT_KITCHEN_ADD(BillCode, ItemCode, ItemName, Quantity, KEEP_PACK, MEMO_DETAIL)

            End If
        Next
    End Sub
    Private Sub NewOrder()
        LoadBillDetail(0)
        TBL_CODE = ""
        CustomerCode = ""
        BillCode = 0

        SVCAmt = 0
        DiscountAmt = 0
        TaxAmt = 0
        Total = 0
        TipAmt = 0
        SubTotal = 0
        EmployeeCode = ""
        txtCustomer.Text = ""
        If IsNumeric(txtBILL_NUM.Text) Then
            txtBILL_NUM.Text = CStr("0000" + CStr(CInt(txtBILL_NUM.Text) + 1)).Substring(0, 5)
        Else
            txtBILL_NUM.Text = "00001"
        End If
    End Sub
    Private Sub UpdateTotal()
        GridOrder.DataSource = dtBillDetail
        SubTotal = GridOrder.GetTotal(GridOrder.RootTable.Columns("TOTAL_ORG"), AggregateFunction.Sum)
        vlSubTotal.Text = String.Format(PublicVariable.NumberFormatString, SubTotal)
        If PublicVariable.SCOption <> "0" And SVCAmt = 0 Then
            SVCAmt = CalculateSC()
        End If

        vlServiceCharge.Text = String.Format(PublicVariable.NumberFormatString, SVCAmt)
        DiscountAmt = GridOrder.GetTotal(GridOrder.RootTable.Columns("DSC_AMT"), AggregateFunction.Sum)
        vlDiscount.Text = String.Format(PublicVariable.NumberFormatString, DiscountAmt)
        TaxAmt = GridOrder.GetTotal(GridOrder.RootTable.Columns("VAT_AMT"), AggregateFunction.Sum)
        vlTax.Text = String.Format(PublicVariable.NumberFormatString, TaxAmt)
        Total = SubTotal + SVCAmt + TaxAmt - DiscountAmt
        vlAmountDue.Text = String.Format(PublicVariable.NumberFormatString, Total)
        GridOrder.ExpandRecords()
    End Sub
    Public Sub LoadBillDetail(ByVal BillCode As Int64)
        Me.BillCode = BillCode
        dtBillDetail = oOrder.LoadBillDetail(BillCode)

        If dtBillDetail.Rows.Count > 0 Then
            Me.TBL_CODE = dtBillDetail.Rows(0).Item("TBL_CODE")
            Me.CustomerCode = dtBillDetail.Rows(0).Item("CUST_CODE").ToString
            Me.EmployeeCode = dtBillDetail.Rows(0).Item("BILL_EMPCODE").ToString
            Me.txtCustomer.Text = dtBillDetail.Rows(0).Item("CUS_NAME").ToString
            Me.txtBILL_NUM.Text = dtBillDetail.Rows(0).Item("BILL_SEQ").ToString
            TipAmt = dtBillDetail.Rows(0).Item("BILL_TIP")
            PRO_CODE = dtBillDetail.Rows(0).Item("PRO_CODE").ToString
            txtGuest.Text = dtBillDetail.Rows(0).Item("BILL_GUEST").ToString
            EmployeeCode = dtBillDetail.Rows(0).Item("BILL_EMPCODE").ToString
            SVCAmt = dtBillDetail.Rows(0).Item("BILL_SVC")
            'BillDeposit = IIf(dtBillDetail.Rows(0).Item("BILL_DEPOSIT") Is DBNull.Value, 0, dtBillDetail.Rows(0).Item("BILL_DEPOSIT"))
            txtSalesDate.Text = CDate(dtBillDetail.Rows(0).Item("BILL_DATE")).ToString(PublicVariable.DateTimeFormat)
        End If
        UpdateTotal()
    End Sub
    Private Sub txtBarcode_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBarcode.KeyDown
        Try


            If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
                If Not IsNumeric(PublicVariable.BarcodeItem) Or _
                        Not IsNumeric(PublicVariable.BarcodeEmployee) Or _
                        Not IsNumeric(PublicVariable.BarcodeCustomer) Then
                    SystemFunction.ShowMessageForm("THE BARCODE LENGTH IS NOT NUMERIC!", "")
                    Return
                End If
                '-------------------------------------SEARCH ITEM------------------------
                If txtBarcode.Text.Length = CInt(PublicVariable.BarcodeItem) Then
                    Dim dt As DataTable
                    Dim arName As New ArrayList
                    arName.Add("@Condition")
                    arName.Add("@Parameter")
                    Dim arVal As New ArrayList
                    arVal.Add(3)
                    arVal.Add(txtBarcode.Text)
                    dt = BLL.ObjectGetBy("ITEM_SEARCH", arName, arVal)

                    If dt.Rows.Count = 1 Then
                        'IF return only 1 record, add to grid
                        AddItem(dt.Rows(0).Item("ITEM_TYPE"), dt.Rows(0).Item("ITEM_CODE").ToString, _
                            dt.Rows(0).Item("ITEM_NAME").ToString, dt.Rows(0).Item("PRICE"), _
                            dt.Rows(0).Item("VAT_RATE"), dt.Rows(0).Item("ATTRIBUTE").ToString, dt.Rows(0).Item("ITEM_INV"), dt.Rows(0).Item("ITEM_STOCK"), 0)
                    ElseIf dt.Rows.Count > 1 Then
                        'if return more than 1 record, show the selection
                        Dim frm As New frmSEARCH_ITEM
                        frm.GridItem.DataSource = dt.DefaultView
                        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                            If frm.GridItem.CurrentRow Is Nothing Then Return
                            Dim dr As DataRowView = CType(frm.GridItem.CurrentRow.DataRow, DataRowView)
                            AddItem(dr.Item("ITEM_TYPE"), dr.Item("ITEM_CODE").ToString, _
                                    dr.Item("ITEM_NAME").ToString, dr.Item("PRICE"), _
                                    dr.Item("VAT_RATE"), dr.Item("ATTRIBUTE").ToString, dt.Rows(0).Item("ITEM_INV"), dt.Rows(0).Item("ITEM_STOCK"), 0)
                        End If
                    Else
                        'not found
                        SystemFunction.ShowMessageForm("ITEM NOT FOUND!", "")
                    End If
                End If
                '-------------------------------------SEARCH EMPLOYEE------------------------
                If txtBarcode.Text.Length = CInt(PublicVariable.BarcodeEmployee) Then
                    Dim dt As DataTable
                    dt = BLL.ObjectGetAll("EMPLOYEE_LOADALL")
                    Dim dv As New DataView(dt)
                    dv.RowFilter = "EMP_CARD='" + txtBarcode.Text + "'"
                    If dv.Count = 1 Then
                        EmployeeCode = dv(0).Item("EMP_CODE").ToString
                        'txtEmployee.Text = dv(0).Item("EMP_NAME").ToString
                    ElseIf dv.Count > 1 Then
                        Dim frm As New frmEMPLOYEE
                        frm.GridEmployee.DataSource = dv
                        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                            If frm.GridEmployee.CurrentRow Is Nothing Then Return
                            Dim dr As DataRowView = CType(frm.GridEmployee.CurrentRow.DataRow, DataRowView)
                            EmployeeCode = dr.Item("EMP_CODE").ToString
                            'txtEmployee.Text = dr.Item("EMP_NAME").ToString
                        End If
                    Else
                        'not found
                        SystemFunction.ShowMessageForm("EMPLOYEE NOT FOUND!", "")
                    End If
                End If
                '-------------------------------------SEARCH CUSTOMER------------------------
                If txtBarcode.Text.Length = CInt(PublicVariable.BarcodeCustomer) Then
                    Dim arName As New ArrayList
                    arName.Add("@Condition")
                    arName.Add("@Parameter")
                    Dim arVal As New ArrayList
                    arVal.Add(5)
                    arVal.Add(txtBarcode.Text)
                    Dim dt As DataTable = BLL.ObjectGetBy("CUSTOMER_SEARCH", arName, arVal)

                    If dt.Rows.Count = 1 Then
                        'if result is 1 record
                        CustomerCode = dt.Rows(0).Item("CUS_CODE").ToString
                        txtCustomer.Text = dt.Rows(0).Item("CUS_NAME").ToString
                    ElseIf dt.Rows.Count > 1 Then
                        'if Result is more than 1 record
                        Dim frm As New frmSEARCH_CUSTOMER
                        frm.GridCustomer.DataSource = dt.DefaultView
                        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                            If frm.GridCustomer.CurrentRow Is Nothing Then Return
                            Dim dr As DataRowView = CType(frm.GridCustomer.CurrentRow.DataRow, DataRowView)
                            CustomerCode = dr.Item("CUS_CODE").ToString
                            txtCustomer.Text = dr.Item("CUS_NAME").ToString
                        End If
                    Else
                        'not found
                        SystemFunction.ShowMessageForm("CUSTOMER NOT FOUND!", "")
                    End If
                End If
                txtBarcode.Text = ""
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Function CalculateSC() As Double
        Select Case PublicVariable.SCOption
            Case "1"
                Return SubTotal * CDbl(PublicVariable.SCPercent) / 100
            Case "2"
                Return (SubTotal - DiscountAmt) * CDbl(PublicVariable.SCPercent) / 100
            Case "3"
                Return Total * CDbl(PublicVariable.SCPercent) / 100
            Case Else
                Return 0
        End Select
    End Function
#End Region

    
    Private Sub txtBarcode_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBarcode.TextChanged

    End Sub
End Class